﻿using FastCloud.Base;
using FastCloud.Base.Config;
using FastCloud.Base.Desgin;
using FastUtil.Convert;
using FastUtil.Extra;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

namespace FastCloud.Controllers.DMP
{
    [Route("frame/dmp/cache")]
    public class CacheController : BaseController
    {

        [Route("list")]
        public IActionResult List()
        {
            return View("~/Views/DMP/Cache/Cache_List.cshtml");
        }

        [Route("getCacheList")]
        public ActionResult GetCacheList()
        {
            string CacheName = ValueConvert.ConvertToString(GetFormParam("CacheName"));
            int PageIndex = ValueConvert.ConvertToInt32(CloudUtil.GetFormParam("PageIndex"));
            int PageSize = ValueConvert.ConvertToInt32(CloudUtil.GetFormParam("PageSize"));
            List<Type> CacheTypeList = InjectContext.GetRegistType(typeof(BaseCacheML<>));
            List<Type> FilterCacheTypeList = CacheTypeList.Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList();
            JArray array = new JArray();
            foreach (Type cache in FilterCacheTypeList)
            {
                Object CacheML = InjectContext.Resolve(cache);
                string FindCacheName = ValueConvert.ConvertToString(cache.GetRuntimeProperty("TableCacheKey").GetValue(CacheML));
                if (!string.IsNullOrEmpty(CacheName))
                {
                    if (FindCacheName.Contains(CacheName))
                    {
                        JObject obj = new JObject();
                        obj.Add("CacheName", FindCacheName);
                        obj.Add("CacheCount", RedisUtil.GetListHashCount(FindCacheName, CloudUtil.GetRedisCacheDBNum()));
                        array.Add(obj);
                    }
                }
                else
                {
                    JObject obj = new JObject();
                    obj.Add("CacheName", FindCacheName);
                    obj.Add("CacheCount", RedisUtil.GetListHashCount(FindCacheName, CloudUtil.GetRedisCacheDBNum()));
                    array.Add(obj);
                }
            }
            Page page = new Page();
            page.Rows = array;
            page.Total = CacheTypeList.Count;
            return Result.OK(page);
        }

        [Route("refreshCache")]
        public ActionResult ClearCache()
        {
            string CacheName = ValueConvert.ConvertToString(GetFormParam("CacheName"));
            RedisUtil.DelteKey(CacheName, CloudUtil.GetRedisCacheDBNum());
            List<Type> CacheTypeList = InjectContext.GetRegistType(typeof(BaseCacheML<>));
            foreach (Type cache in CacheTypeList)
            {
                Object obj = InjectContext.Resolve(cache);
                string FindCacheName = ValueConvert.ConvertToString(cache.GetRuntimeProperty("TableCacheKey").GetValue(obj));
                if (FindCacheName== CacheName)
                {
                    MethodInfo method = cache.GetMethod("InitCache", new Type[] { });
                    object[] parameters = new object[] { };
                    method.Invoke(obj, parameters);
                }
            }
            return Result.OK();
        }
    }
}
